package com.example.equipment.controller;

import com.example.equipment.annotation.IgnoreCheck;
import com.example.equipment.bean.ProductMac;
import com.example.equipment.bean.enums.ResultEnum;
import com.example.equipment.bean.User;
import com.example.equipment.bean.config.OpenApiResult;
import com.example.equipment.bean.po.BatchPo;
import com.example.equipment.bean.po.CheckMacPo;
import com.example.equipment.service.ProductMacService;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 散热器测试记录处理
 */
@RestController
@Slf4j
@RequestMapping("/api/mac")
public class ProductMacController {

    @Autowired
    private ProductMacService productMacService;

    private Gson gson = new GsonBuilder().create();

    /**
     * 导入sn号
     *
     * @param request
     * @return
     */
    @PostMapping("/import")
    public OpenApiResult importMac(@RequestParam("file") MultipartFile file,
                                   @RequestParam(value = "productId") Integer productId,
                                   HttpServletRequest request) {
        log.info("==> ProductMacController.importMac(). productId={}", productId);

        //校验文件格式
        if (!file.getOriginalFilename().endsWith(".txt")
                && !file.getOriginalFilename().endsWith(".TXT")) {
            return new OpenApiResult(ResultEnum.FILE_TYPE_EXCEPTION);
        }
        User user = (User) request.getSession().getAttribute("user");
        try {
            return productMacService.importMac(file, productId, user);
        } catch (Exception e) {
            log.error("批量异常，e=", e);
            return OpenApiResult.fail();
        }
    }

    @IgnoreCheck
    @PostMapping("/check")
    public OpenApiResult checkMac(@RequestBody CheckMacPo po) {
        log.info("==> ProductMacController.cleanById(). po={}", po == null ? "{}" : gson.toJson(po));
        if (StringUtils.isEmpty(po.getProductNo())
            || StringUtils.isEmpty(po.getSerialNo())
            || StringUtils.isEmpty(po.getMacAddress())) {
            return new OpenApiResult(ResultEnum.REQUESTPARAMERROR);
        }
        return productMacService.checkMac(po);
    }

    /**
     * 清除mac地址信息
     *
     * @param
     * @return
     */
    @PostMapping("/clean")
    public OpenApiResult cleanById(@RequestBody BatchPo po, HttpServletRequest request) {
        log.info("==> ProductMacController.cleanById(). id.size()={}", po.getId().size());
        if (po == null || CollectionUtils.isEmpty(po.getId())) {
            return new OpenApiResult(ResultEnum.REQUESTPARAMERROR);
        }
        User user = (User) request.getSession().getAttribute("user");
        productMacService.cleanById(po, user.getName());
        return OpenApiResult.ok();
    }

    @PostMapping("/delete")
    public OpenApiResult deleteById(@RequestBody BatchPo po, HttpServletRequest request) {
        log.info("==> ProductMacController.deleteById(). id.size()={}", po.getId().size());
        if (po == null || CollectionUtils.isEmpty(po.getId())) {
            return new OpenApiResult(ResultEnum.REQUESTPARAMERROR);
        }
        User user = (User) request.getSession().getAttribute("user");
        productMacService.deleteById(po, user.getName());
        return OpenApiResult.ok();
    }

    /**
     * 获取测试记录列表
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    public OpenApiResult<PageInfo<ProductMac>> getList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize,
                                                    @RequestParam(value = "productNo", required = false) String productNo,
                                                    @RequestParam(value = "productName", required = false) String productName,
                                                    @RequestParam(value = "serialNo", required = false) String serialNo) {
        log.info("==> ProductMacController.getList(). productNo={},productName={}, serialNo={}", productNo, productName, serialNo);
        PageInfo<ProductMac> info = PageHelper.startPage(page, pageSize).doSelectPageInfo((ISelect) () -> {
            productMacService.getList(productNo, productName, serialNo);
        });

        return OpenApiResult.ok(info);
    }
}
